package com.shugl.demo.utils;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

/**
 * 系统工具
 * @date 2020/5/11
 * @author shugl
 */
public class SysUtil {

    public static final String CLASS_PATH = getWebInfPath();

    private static final String ZIP = "zip";
    private static final String FILE = "file";
    private static final String JAR = "jar";

    /**
     * 获取项目绝对路径
     * @return
     */
    public static String getWebInfPath() {
        URL url = SysUtil.class.getProtectionDomain().getCodeSource().getLocation();
        String path = url.toString();

        if (path.startsWith(ZIP)) {
            //当class文件在war中时，此时返回zip:D:/...这样的路径
            path = path.substring(4);
        } else if (path.startsWith(FILE)) {
            path = path.substring(6);
        } else if (path.startsWith(JAR)) {
            path = path.substring(10);
        }
        try {
            path =  URLDecoder.decode(path, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return path;
    }
}
